
#include "adc.h"
#include "hc32f4xx_conf.h"
#include "hc32_ll_adc.h"
#include "hc32_common.h"
#include "hc32_ll.h" 
#include "baseData.h" 

/* ADC clock selection definition. */
#define ADC_CLK_PCLK                (1u)
#define ADC_CLK_MPLLQ               (2u)
#define ADC_CLK_UPLLR               (3u)

/* ADC unit instance for this example. */
#define ADC_UNIT                        (CM_ADC1)
#define ADC_PERIPH_CLK                  (FCG3_PERIPH_ADC1)
/* ADC sequence to be used. */
#define ADC_EOC_FLAG                    (ADC_FLAG_EOCA)
#define ADC_SEQ                         (ADC_SEQ_A)
#define ADC_CH                          (ADC_CH9)

 
#define ADC1_SA_CHANNEL_COUNT       (1u)
#define ADC1_PORT                   (GPIO_PORT_B)
#define ADC1_PIN                    (GPIO_PIN_01)
#define ADC1_CHANNEL                (ADC1_SA_CHANNEL)


/* Select MPLLQ as ADC clock. */
#define ADC_CLK                     (ADC_CLK_UPLLR) 


/* ADC reference voltage. The voltage of pin VREFH. */
#define ADC_VREF                        (3.3F)
#define ADC_ACCURACY                    (1UL << 12U)
#define ADC_CAL_VOL(adcVal)             (uint16_t)((((float32_t)(adcVal) * ADC_VREF) / ((float32_t)ADC_ACCURACY)) * 1000.F)
  void AdcSetPinAnalogMode(void)
{
    stc_gpio_init_t stcGpioInit;

    (void)GPIO_StructInit(&stcGpioInit);
    stcGpioInit.u16PinAttr = PIN_ATTR_ANALOG;
    (void)GPIO_Init(ADC1_PORT, ADC1_PIN, &stcGpioInit);
}



  void AdcClockConfig(void)
{	
    stc_clock_xtal_init_t stcXtalInit;    
	stc_clock_pllx_init_t stcUpllInit;
	
    MEM_ZERO_STRUCT(stcXtalInit);
    MEM_ZERO_STRUCT(stcUpllInit);
	
    /* Use XTAL as UPLL source. */
    stcXtalInit.u8State = ENABLE;
    stcXtalInit.u8Mode = CLK_XTAL_MD_OSC;
    stcXtalInit.u8Drv = CLK_XTAL_DRV_LOW;
    CLK_XtalInit(&stcXtalInit);
    CLK_XtalCmd(ENABLE);

	//stcUpllInit->PLLCFGR_f.PLLSRC = CLK_PLL_SRC_XTAL;
    /* Set UPLL out 240MHz. */
	stcUpllInit.PLLCFGR_f.PLLM = (2UL);
	/* upll = 8M(XTAL) / pllmDiv * plln */
    stcUpllInit.PLLCFGR_f.PLLN = (60UL);

    stcUpllInit.PLLCFGR_f.PLLP = 16u;
	stcUpllInit.PLLCFGR_f.PLLQ = 16u;
	stcUpllInit.PLLCFGR_f.PLLR = 16u;
    //CLK_SetPllSource(ClkPllSrcXTAL);
	 
    (void)CLK_PLLxInit(&stcUpllInit);
    CLK_PLLxCmd(ENABLE);
    CLK_SetPeriClockSrc(CLK_PERIPHCLK_PLLXP); 
}



  void AdcInitConfig(void)
{
    stc_adc_init_t stcAdcInit; 		
									
    MEM_ZERO_STRUCT(stcAdcInit);	
					
	/* 1. Enable ADC peripheral clock. */
    FCG_Fcg3PeriphClockCmd(ADC_PERIPH_CLK, ENABLE);

    /* 2. Modify the default value depends on the application. Not needed here. */
    (void)ADC_StructInit(&stcAdcInit);

    /* 3. Initializes ADC. */
    (void)ADC_Init(ADC_UNIT, &stcAdcInit);
}


  void AdcChannelConfig(void)
{
	AdcSetPinAnalogMode();
	
	/* 4.2 Enable ADC channels. Call ADC_ChCmd() again to enable more channels if needed. */
	ADC_ChCmd(ADC_UNIT, ADC_SEQ, ADC_CH, ENABLE);
	/* 5. Set sampling time if needed. */
	ADC_SetSampleTime(ADC_UNIT, ADC_CH, 0x30U);

	/* 6.如果需要，转换数据平均值计算功能。
	如果需要，再次调用ADC_ConvDataAverageChCmd()以使能更多平均通道。 */
	//ADC_ConvDataAverageConfig(ADC_UNIT, ADC_AVG_CNT8);
	//ADC_ConvDataAverageChCmd(ADC_UNIT, ADC_CH, ENABLE);
}

 
void adcInit(void)
{ 
	AdcInitConfig();
	AdcChannelConfig();
	ADC_Start(ADC_UNIT);
}
    

void readAdc(void)
{   
	static uint16_t u16AdcValue,u16AdcValue2; 
	if (ADC_GetStatus(ADC_UNIT, ADC_EOC_FLAG) == SET) 
	{
		ADC_ClearStatus(ADC_UNIT, ADC_EOC_FLAG);
		u16AdcValue = ADC_GetValue(ADC_UNIT, ADC_CH);
		u16AdcValue2=3*ADC_CAL_VOL(u16AdcValue);
		DeviceInfo.voltage=u16AdcValue2/100;
	}
	//ADC_Stop(ADC_UNIT);
}
